import { message } from 'antd';
import {
  list, echo, allow, deny, edit,
  listSpecial, dele, create, upload,
} from '../services/verify';
import tools from '../../../utils';
import moment from 'moment';
import 'moment/locale/zh-cn';

moment.locale('zh-cn');

const types = require('../../../assets/stureg_web/static/types.json');

export default {
  namespace: 'verify',

  state: {
    data: [],
    total: 0,
    success: true,
    echoModel: {},
    dataSpecial: [], // 开设专业数据(树数据)
    sysData: [], // 系部数据
    specData: {}, // 专业数据
    mappingSpecialName: {}, // 根据专业id获取专业名称
    mappingSpecialPID: {}, // 根据专业id获取系部id
    uploadUrl: "", // 文件上传成功后返回的网络地址
    errMsg: '', // 错误信息
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response });
    },
    *echo({ payload }, { call, put }) {
      const response = yield call(echo, payload);
      yield put({ type: 'callbackEcho', response });
    },
    *allow({ payload }, { call, put }) {
      const response = yield call(allow, payload);
      yield put({ type: 'callbackSave', response });
    },
    *deny({ payload }, { call, put }) {
      const response = yield call(deny, payload);
      yield put({ type: 'callbackSave', response });
    },
    *edit({ payload }, { call, put }) {
      const response = yield call(edit, payload);
      yield put({ type: 'callbackSave', response });
    },
    *listSpecial({ payload }, { call, put }) {
      const response = yield call(listSpecial, payload);
      yield put({ type: 'callbackListSpecial', response });
    },
    *dele({ payload }, { call, put }) {
      const response = yield call(dele, payload);
      yield put({ type: 'callbackSave', response });
    },
    *create({ payload }, { call, put }) {
      const response = yield call(create, payload);
      yield put({ type: 'callbackSave', response });
    },
    *upload({ payload }, { call, put }) {
      const response = yield call(upload, payload);
      yield put({ type: 'callbackUpload', response });
    },
  },

  reducers: {
    callbackList(st, { response }) {
      const state = st;
      if (response.success) {
        state.total = response.total;
        state.data = response.root
          .map((item) => {
            const { specialId, type, signupTime } = item;
            let specialName = state.mappingSpecialName[specialId];
            specialName += ' - ' + types[type];

            return {
              ...item,
              specialName,
              signupTime: moment(signupTime).fromNow(),
            };
          });
      }
      return { ...state };
    },
    callbackSave(st, { response }) {
      const { success, status, msg } = response;
      const state = st;
      state.success = success;
      state.errMsg = '';

      if (success) {
        if (!status) message.success(msg);
        else state.errMsg = msg;
      } else if (status === 1) message.error(msg);

      return { ...state };
    },
    callbackEcho(st, { response }) {
      const state = st;
      if (response.success) {
        state.echoModel = response.model;
        state.echoModel.auditAdminName = response.auditAdminName;
      }
      return { ...state };
    },
    callbackListSpecial(st, { response }) {
      const state = st;
      if (response.success) {
        const { data } = response;

        state.sysData = [];
        state.specData = {};
        const allData = [];
        const mappingSpecialName = {}; // 根据专业id获取专业名称
        const mappingSpecialPID = {}; // 根据专业id获取系部id

        for (let i = 0; i < data.length; i += 1) {
          const item = data[i];
          const { id, pid, name } = item;
          const isSpec = pid > 0; // 系部或专业: 0系部, >0专业
          mappingSpecialName[id] = name;
          if (isSpec) {
            if (!state.specData[pid]) state.specData[pid] = [];
            state.specData[pid].push(item);
            mappingSpecialName[id] = name;
            mappingSpecialPID[id] = pid;
          } else state.sysData.push(item);

          allData.push({
            ...item,
            key: id,
            title: name,
            selectable: isSpec, // 专业树节点可供用户点选, 而系部不可选择
          });
        }

        state.mappingSpecialName = mappingSpecialName;
        state.mappingSpecialPID = mappingSpecialPID;
        state.dataSpecial = tools.funcPidToChildren(allData);
      }
      return { ...state };
    },
    callbackUpload(st, { response }) {
      const state = st;
      state.success = response.success;
      if (response.success) state.uploadUrl = response.imageUrl;
      return { ...state };
    },
  },
};
